const seq = require('./seq')
const moment = require('moment');
const { QueryTypes } = require('sequelize');
class Role {

  async updateRoleByID(Role) {
    const {id,name,description,state} = Role
    const currentTime = moment().format('YYYY-MM-DD HH:mm:ss');
    const sql = `update  role 
                set name=?,description=?,state=?, updatedtime=? where id=${id}` 
    const results = await seq.query(sql,
      {
        replacements: [name,description,state,currentTime],
        type: QueryTypes.UPDATE
      }
    )
    return results
}

 async addRole(Role) {
  const {name,description,state} = Role
  console.log("role:",Role)
  const sql = `insert into role 
              set name=?,description=?,state=?`
  console.log("sql:",sql)
  const results = await seq.query(sql,
    {
      replacements: [name,description,state],
      type: QueryTypes.INSERT
    }
  )
  return results
}
  async getRoleDetail(id) {
    const sql = `SELECT *
                FROM role 
                where id=${id} and isDelete=0`
    const results = await seq.query(sql,
      {
        type: QueryTypes.SELECT
      }) 
    return results
  }

  async delRoleByid(id) {
    const sql = `update role set isDelete=1 where id=${id}`
    const results = await seq.query(sql,
      {
        type: QueryTypes.UPDATE
      }) 
    return results
  }

  //需要增加分页查询,page：当前页码，pagesize;当前页显示的条数
  async getAll(params) {
    const {page,pageSize} = params
    const start = (page-1)*pageSize
    const limit = pageSize
    const sql = `SELECT * from role where isDelete=0 limit ${start},${limit}`
    const results = await seq.query(sql,
      {
        type: QueryTypes.SELECT
      }) 
    return results
  }
  async getRoleCount() {
    const sql = `SELECT count(*) as total from role where isDelete=0`
    const results = await seq.query(sql,
      {
        type: QueryTypes.SELECT
      })
      return results
  }
}

module.exports = new Role()